import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:ui';

class ScreenUtil {
  /// 默认设计稿尺寸（单位 dp 或 pt）
  static double _designW = 375.0;
  static double _designH = 667.0;
  static bool _allowFontScaling = false;

  /// 配置设计稿尺寸
  /// Configuration design draft size
  /// [w] 设计稿宽度 Design draft width
  /// [h] 设计稿高度 Design draft height
  /// [allowFontScaling] 是否允许字体根据系统缩放 Whether to allow font scaling according to system settings
  static void setDesignWHD(double w, double h, {bool allowFontScaling = false}) {
    _designW = w;
    _designH = h;
    _allowFontScaling = allowFontScaling;
  }

  /// 当前设备屏幕宽度
  /// Current device screen width
  static double get width {
    final view = PlatformDispatcher.instance.views.first;
    return MediaQueryData.fromView(view).size.width;
  }

  /// 当前设备屏幕高度
  /// Current device screen height
  static double get height {
    final view = PlatformDispatcher.instance.views.first;
    return MediaQueryData.fromView(view).size.height;
  }

  /// 当前设备像素密度
  /// Current device pixel density
  static double get scale {
    final view = PlatformDispatcher.instance.views.first;
    return MediaQueryData.fromView(view).devicePixelRatio;
  }

  /// 当前设备文字缩放因子
  /// Current device text scale factor
  static double get textScaleFactor {
    final view = PlatformDispatcher.instance.views.first;
    return MediaQueryData.fromView(view).textScaler.scale(1.0);
  }

  /// 导航栏高度（状态栏 + 工具栏）
  /// Navigation bar height (status bar + toolbar)
  static double get navigationBarHeight {
    final view = PlatformDispatcher.instance.views.first;
    final mediaQuery = MediaQueryData.fromView(view);
    return mediaQuery.padding.top + kToolbarHeight;
  }

  /// 顶部安全区域高度
  /// Top safe area height
  static double get topSafeHeight {
    final view = PlatformDispatcher.instance.views.first;
    return MediaQueryData.fromView(view).padding.top;
  }

  /// 底部安全区域高度
  /// Bottom safe area height
  static double get bottomSafeHeight {
    final view = PlatformDispatcher.instance.views.first;
    return MediaQueryData.fromView(view).padding.bottom;
  }

  /// 更新状态栏样式
  /// Update the status bar style
  /// [style] 状态栏样式 Status bar style
  static void updateStatusBarStyle(SystemUiOverlayStyle style) {
    SystemChrome.setSystemUIOverlayStyle(style);
  }

  /// 宽度缩放因子
  /// Width scaling factor
  static double get scaleWidth => width / _designW;

  /// 高度缩放因子
  /// Height scaling factor
  static double get scaleHeight => height / _designH;

  /// 根据设计稿宽度进行缩放
  /// Scale according to design draft width
  /// [width] 设计稿中的宽度值 Width value in design draft
  static double setWidth(double width) {
    return width * scaleWidth;
  }

  /// 根据设计稿高度进行缩放
  /// Scale according to design draft height
  /// [height] 设计稿中的高度值 Height value in design draft
  static double setHeight(double height) {
    return height * scaleHeight;
  }

  /// 字体大小适配方法
  /// Font size adaptation method
  /// [fontSize] UI设计上字体的大小,单位px
  /// The size of the font on the UI design, in px
  static double setSp(double fontSize) {
    final view = PlatformDispatcher.instance.views.first;
    final mediaQuery = MediaQueryData.fromView(view);
    return _allowFontScaling
        ? (fontSize * scaleWidth)
        : ((fontSize * scaleWidth) / mediaQuery.textScaler.scale(1.0));
  }
}
